from flask import Flask, render_template, request
from question_generator import generate_exercise

app = Flask(__name__)

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/preview', methods=['POST'])
def preview():
    # 基本设置
    operations = request.form.getlist('operations[]')
    if not operations:  # 如果没有选择任何运算类型，默认使用加法
        operations = ['addition']
    
    difficulty = request.form.get('difficulty')
    count = request.form.get('count')
    
    # 布局设置
    layout = request.form.get('layout', '2')
    font_size = request.form.get('font_size', 'md')
    line_spacing = request.form.get('line_spacing', 'md')
    
    # 学生信息设置
    show_student_info = request.form.get('show_student_info') == '1'
    name_label = request.form.get('name_label', '姓名：')
    class_label = request.form.get('class_label', '班级：')
    date_label = request.form.get('date_label', '日期：')
    
    # 生成题目
    questions, answers = generate_exercise(operations, difficulty, count)
    
    return render_template('preview.html', 
                         questions=questions, 
                         answers=answers,
                         operations=operations,
                         difficulty=difficulty,
                         count=count,
                         layout=layout,
                         font_size=font_size,
                         line_spacing=line_spacing,
                         show_student_info=show_student_info,
                         name_label=name_label,
                         class_label=class_label,
                         date_label=date_label)

if __name__ == '__main__':
    app.run(debug=True)
